const { defineConfig } = require("@vue/cli-service");
const { list } = require("./src/mock");
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares(middle, { app }) {
      app.get("/api/list", (req, res) => {
        const { pageCode = 1, pageSize = 10 } = req.query;

        if (pageCode * pageSize <= list.length) {
          res.send({
            code: 200,
            data: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
            message: '数据加载成功'
          });
        } else {
          res.send({
            code: 400,
            data: null,
            message: '没有更多'
          });
        }
      });

      app.get('/api/list/:id', (req, res) => {
        const { id } = req.params;
        const result = list.find(v => v.id === id)
        if(result) {
          res.send({
            code: 200,
            data: result,
            message: '请求数据成功'
          })
        } else {
          res.send({
            code: 400,
            data: null,
            message: '该商品已经下架！'
          })
        }
      })
      return middle;
    },
  },
});
